﻿using System;
using System.IO;
using System.Windows.Forms;
using CC.Utilities;

namespace CC.Yacht.Windows.Forms
{
    public partial class ChatPanel : UserControl
    {
        #region Constructor
        public ChatPanel()
        {
            InitializeComponent();
            ShowTimestamps = true;
        }

        ~ChatPanel()
        {
            Dispose();
        }
        #endregion

        #region Private Fields
        private SoundPlayer _SoundPlayer;
        #endregion

        #region Public Events
        public event EventHandler<SendMessageEventArgs> SendingMessage;
        #endregion

        #region Public Properties
        public bool AllowSounds { get; set; }

        public bool ShowTimestamps { get; set; }
        #endregion

        #region Private Event Handlers
        private void _ButtonSendMessage_Click(object sender, EventArgs e)
        {
            SendMessage();
        }

        private void _TextBoxOutbound_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;

                SendMessage();
            }
        }

        private void _TextBoxOutbound_TextChanged(object sender, EventArgs e)
        {
            _ButtonSendMessage.Enabled = (_TextBoxOutbound.TextLength > 0);
        }
        #endregion

        #region Private Methods
        private void AppendText(string text)
        {
            try
            {
                _TextBoxInbound.AppendTextEx((ShowTimestamps ? "[" + DateTime.Now.ToString("HH:mm") + "] " : string.Empty) + text);
            }
            catch (ObjectDisposedException exception)
            {
                Logging.LogException(exception);
            }
        }

        private void PlaySound(Stream stream)
        {
            if (_SoundPlayer == null)
            {
                _SoundPlayer = new SoundPlayer(this);
            }

            _SoundPlayer.Play(stream);
        }

        private void SendMessage()
        {
            OnSendingMessage(new SendMessageEventArgs(_TextBoxOutbound.Text));
            
            _TextBoxOutbound.Clear();
        }
        #endregion

        #region Protected Methods
        protected void OnSendingMessage(SendMessageEventArgs eventArgs)
        {
            if (SendingMessage != null)
            {
                SendingMessage(this, eventArgs);
            }
        }
        #endregion

        #region Public Methods
        public void AddAction(string message)
        {
            AppendText("* " + message);
        }

        public void AddMessage(User user, string message)
        {
            if (AllowSounds && message.StartsWith("!"))
            {
                switch (message.ToLower().Trim())
                {
                    case "!chopchop":
                        {
                            PlaySound(Yacht.Properties.Resources.Chop_Chop);
                            break;
                        }
                    case "!ding":
                        {
                            PlaySound(Yacht.Properties.Resources.Ding);
                            break;
                        }
                }
            }

            AppendText(user.Username + ": " + message);
        }

        public void Clear()
        {
            _TextBoxInbound.Clear();
            _TextBoxOutbound.Clear();
        }

        public new void Dispose()
        {
            if (_SoundPlayer != null)
            {
                _SoundPlayer.Dispose();
                _SoundPlayer = null;
            }

            base.Dispose();
        }
        #endregion
    }
}
